<template>
  <div class="mod-user">
    <el-form label-width="200px" 
      ref="dataForm" :model="dataForm" :rules="dataRule" @keyup.enter.native="submit()">
      <el-form-item label="规则名称">
        <el-input v-model="dataForm.ruleName" placeholder="规则名称" clearable style="width: 300px"></el-input>
      </el-form-item>
      <el-form-item label="最小年龄" prop="ageMin" >
        <el-input v-model="dataForm.ageMin" placeholder="最小年龄" clearable style="width: 300px"></el-input>
      </el-form-item>
      <el-form-item label="最大年龄" prop="ageMax">
        <el-input v-model="dataForm.ageMax" placeholder="最大年龄" clearable style="width: 300px"></el-input>
      </el-form-item>
      <el-form-item label="最小额度">
        <el-input v-model="dataForm.balanceMin" placeholder="最小额度" clearable style="width: 300px"></el-input>
      </el-form-item>
      <el-form-item label="是否禁止失信人员">
        <el-switch v-model="dataForm.dishonestValue"></el-switch>
      </el-form-item>
      <el-form-item label="是否禁止无工作人员">
        <el-switch v-model="dataForm.jobValue"></el-switch>
      </el-form-item>
      <el-form-item label="是否需要禁止有逾期人员">
        <el-switch v-model="dataForm.overdueValue"></el-switch>
      </el-form-item>
      <!-- <el-form-item label="是否开启此条规则">
        <el-switch v-model="dataForm.start"></el-switch>
      </el-form-item> -->
      <el-form-item>
        <el-button @click="submit()">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  export default {
    data () {
      var checkAgeMax = (rule, value, callback) => {
        console.log(value)
        if (value && this.dataForm.ageMin && (this.dataForm.ageMin >= value)) {
          return callback(new Error('最小年龄限制不能大于最大年龄限制'))
        } else {
          callback()
        }
      }
      return {
        dataForm: {
          ageMax: null,
          ageMin: null,
          balanceMin: null,
          dishonestValue: null,
          id: null,
          jobValue: false,
          overdueValue: false,
          ruleName: ''
        },
        dataRule: {
          ageMax: [
            { validator: checkAgeMax, trigger: 'blur' }
          ]
        }
      }
    },
    components: {
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.$http({
          url: this.$http.adornUrl('/app/rules/user/selectRule'),
          method: 'get',
          params: this.$http.adornParams({
            ruleId: 1
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            data.rule.dishonestValue = data.rule.dishonestValue === null || false
            data.rule.overdueValue = data.rule.overdueValue === null || false
            data.rule.jobValue = data.rule.jobValue === null || false

            this.dataForm = data.rule
          } else {
          }
          this.dataListLoading = false
        })
      },
      submit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl('/app/rules/user/update'),
              method: 'post',
              data: this.$http.adornData({
                ageMax: this.dataForm.ageMax,
                ageMin: this.dataForm.ageMin,
                balanceMin: this.dataForm.balanceMin,
                dishonestValue: this.dataForm.dishonestValue || false,
                id: this.dataForm.id,
                jobValue: this.dataForm.jobValue || false,
                overdueValue: this.dataForm.overdueValue || false,
                ruleName: this.dataForm.ruleName
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.getDataList()
                  }
                })
              } else {
              }
              this.dataListLoading = false
            }).catch((error) => {
              console.log(error)
            })
          }
        })
      }
    }
  }
</script>
